import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header

# 邮箱配置
SMTP_SERVER = "smtp.163.com"
SMTP_PORT = 465  # SSL端口
EMAIL = "justforted@163.com"
AUTH_CODE = "HLykuQC6dsZVb7js"


def send_email(to_email, subject, content, content_type="plain"):
    """
    发送邮件
    :param to_email: 收件人邮箱
    :param subject: 邮件主题
    :param content: 邮件内容
    :param content_type: 内容类型 'plain' 或 'html'
    """
    try:
        # 创建邮件对象
        message = MIMEMultipart()
        message["From"] = Header(f"测试发送者 <{EMAIL}>", "utf-8")
        message["To"] = Header(to_email, "utf-8")
        message["Subject"] = Header(subject, "utf-8")

        # 添加邮件内容
        message.attach(MIMEText(content, content_type, "utf-8"))

        # 连接SMTP服务器
        server = smtplib.SMTP_SSL(SMTP_SERVER, SMTP_PORT)
        server.login(EMAIL, AUTH_CODE)

        # 发送邮件
        text = message.as_string()
        server.sendmail(EMAIL, to_email, text)
        server.quit()

        print(f"邮件发送成功！收件人: {to_email}")
        return True

    except Exception as e:
        print(f"邮件发送失败: {str(e)}")
        return False


# 测试数据
if __name__ == "__main__":
    # 测试邮件内容 - HTML表格格式
    test_subject = "AI模型在GPQA数据集上的性能测试结果"
    test_content = """
    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 20px;
                background-color: #f5f5f5;
            }
            .container {
                background-color: white;
                padding: 20px;
                border-radius: 8px;
                box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            }
            h2 {
                color: #333;
                text-align: center;
                margin-bottom: 20px;
            }
            table {
                width: 100%;
                border-collapse: collapse;
                margin: 20px 0;
                box-shadow: 0 2px 8px rgba(0,0,0,0.1);
            }
            th {
                background-color: #4CAF50;
                color: white;
                padding: 12px;
                text-align: center;
                font-weight: bold;
            }
            td {
                background-color: #e8f5e8;
                padding: 12px;
                text-align: center;
                border: 1px solid #ddd;
            }
            tr:nth-child(even) td {
                background-color: #d4f4d4;
            }
            tr:hover td {
                background-color: #c8e6c9;
            }
            .note {
                margin-top: 20px;
                padding: 15px;
                background-color: #f0f8f0;
                border-left: 4px solid #4CAF50;
                border-radius: 4px;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h2>AI模型在GPQA数据集上的性能对比</h2>
            
            <table>
                <thead>
                    <tr>
                        <th>模型名称</th>
                        <th>准确率 (%)</th>
                        <th>F1分数</th>
                        <th>推理时间 (ms)</th>
                        <th>参数量</th>
                    </tr>
                </thead>
                <tbody>
                    <tr>
                        <td><strong>GPT-4o</strong></td>
                        <td>78.5</td>
                        <td>0.782</td>
                        <td>1200</td>
                        <td>~1T</td>
                    </tr>
                    <tr>
                        <td><strong>GPT-4</strong></td>
                        <td>74.2</td>
                        <td>0.738</td>
                        <td>1800</td>
                        <td>~1.7T</td>
                    </tr>
                    <tr>
                        <td><strong>GPT-3.5</strong></td>
                        <td>65.8</td>
                        <td>0.651</td>
                        <td>800</td>
                        <td>175B</td>
                    </tr>
                    <tr>
                        <td><strong>Claude</strong></td>
                        <td>76.3</td>
                        <td>0.759</td>
                        <td>1400</td>
                        <td>~500B</td>
                    </tr>
                </tbody>
            </table>
            
            <div class="note">
                <h3>测试说明：</h3>
                <ul>
                    <li>测试数据集：GPQA (Graduate-Level Google-Proof Q&A Benchmark)</li>
                    <li>测试时间：2024年1月</li>
                    <li>测试环境：标准化API调用</li>
                    <li>评估指标：准确率、F1分数、平均推理时间</li>
                </ul>
                
                <p><strong>结论：</strong>GPT-4o在准确率和F1分数上表现最佳，Claude紧随其后，GPT-4排名第三，GPT-3.5在速度上有优势但准确率相对较低。</p>
            </div>
        </div>
    </body>
    </html>
    """

    # 发送测试邮件（可以发送给自己进行测试）
    recipient = "lizheng@pingpongx.com"  # 收件人邮箱，这里设置为自己的邮箱进行测试

    print("正在发送测试邮件...")
    success = send_email(recipient, test_subject, test_content, content_type="html")

    if success:
        print("测试邮件发送完成！请检查收件箱。")
    else:
        print("测试邮件发送失败，请检查配置。")
